package com.example.myapplication;
import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;


import java.util.List;

public class Adapter extends RecyclerView.Adapter <Adapter.MyViewHolder>{
    private Context context;
    //我还是觉得是这里的context的赋值没正确导致的错误
    private List<Person> personList;

    public Adapter(Context context,List<Person> personList) {
        this.personList = personList;
        this.context=context;

    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        MyViewHolder myViewHolder=new MyViewHolder(itemview);
     //   context = parent.getContext();
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
     Person person=personList.get(position);
     holder.nameText.setText(person.getName());
     holder.phoneText.setText(person.getPhone());
     holder.head.setImageResource(person.getHeadId());
//        if(context==null){
//            Log.d("whl", "1111111111111111111111111111111111111111111");
//        }
//        Intent intent = new Intent(context, MainActivity2.class);
//        intent.putExtra("person_id", "21211"); // 可以传递一些额外的数据
//
//        launcher.launch(intent);

       holder.head.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {

            Log.d("whl", "1111111111111111111111111111111111111111111");

//        Intent intent = new Intent(context, MainActivity2.class);
//        intent.putExtra("person_id", "21211"); // 可以传递一些额外的数据
//    launcher.launch(intent);
           }
       });
    }

    @Override
    public int getItemCount() {
        return personList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView nameText;
        TextView phoneText;
        ImageView head;

        public MyViewHolder(View itemView) {

           super(itemView);
            this.head = itemView.findViewById(R.id.imageView5);
            this.phoneText =itemView.findViewById(R.id.textView5);
            this.nameText =itemView.findViewById(R.id.textView6);
        }
    }

}
